# 面向对象高级编程
class Person():
    def __init__(self, age=12):
        self.age = age
    __slots__ = ('age', 'name', 'pick', 'say')
    def __str__(self):
        return 'name={0}, age={1}'.format(self.name, self.age)
p = Person()
p. name = 'bb'
def say(self):
    pass
from  types import MethodType
p.say = MethodType(say, p)
# p.xx = 'xx' # AttributeError: 'Person' object has no attribute 'xx'

print('$' * 1000)

class Student():
    def __init__(self, age):
        self._age = age
    @property
    def age(self):
        return self._age
    @age.setter
    def age(self, age):
        self._age = age
s = Student(32)
print(s.age)
s.age = 31121
print(s.age)


print('$' * 1000)
# 多重继承
class Male():
    def sex(self):
        print('thick')
class Female():
    def breat(self):
        print('soft')
    def sex(self):
        print('female sex')
class LALA(Female, Male):
    pass

lala = LALA()
lala.sex()
lala.breat()


print('$' * 1000)
# 枚举
from enum import Enum,unique
GENDER = Enum('gender', ('male', 'female'))
print(GENDER['female'])
print(GENDER.male)
print(GENDER(1)) # 通过value获取时，默认value从1开始，如果为0会报错

# 自定义枚举类
@unique
class SEX(Enum):
    male = 0
    female = 1

print(SEX['female'])
print(SEX.male)
print(SEX(0))

print('$' * 1000)
class My_list():
    def __init__(self, list):
        self.index = -1
        self.a = list
    def __iter__(self):
        return self
    def __next__(self):
        i = self.index + 1
        if i == len(self.a):
            raise StopIteration()
        else:
            self.index = self.index + 1
            return self.a[i]
    def __getitem__(self, item):
        if isinstance(item, int):
            if item <= len(self.a):
                return self.a[item]
            else:
                return -1
        elif isinstance(item, slice):
            return self.a[item.start: item.stop]

ml = My_list([5, 6, 7, 8])
for i in ml:
    print('i = {0}'.format(i))

print(ml[3])
print(ml[0:3])